#ifndef _GLESCUBE_H_
#define _GLESCUBE_H_


#include <FBase.h>
#include <FSystem.h>
#include <FGraphics.h>
#include <FUi.h>
#include <FApp.h>
#include <FGraphicsOpengl2.h>


struct Matrix
{
	Osp::Graphics::Opengl::GLfloat m[4][4];
};

class GlesCube :
	public Osp::App::Application,
	public Osp::System::IScreenEventListener,
	public Osp::Ui::IKeyEventListener,
	public Osp::Base::Runtime::ITimerEventListener
{
public:
	static Osp::App::Application* CreateInstance(void);

public:
	GlesCube();
	~GlesCube();


public:
	bool OnAppInitializing(Osp::App::AppRegistry& appRegistry);
	bool OnAppTerminating(Osp::App::AppRegistry& appRegistry, bool forcedTermination = false);
	void OnForeground(void);
	void OnBackground(void);
	void OnLowMemory(void);
	void OnBatteryLevelChanged(Osp::System::BatteryLevel batteryLevel);
	void OnKeyPressed(const Osp::Ui::Control& source, Osp::Ui::KeyCode keyCode);
	void OnKeyReleased(const Osp::Ui::Control& source, Osp::Ui::KeyCode keyCode);
	void OnKeyLongPressed(const Osp::Ui::Control& source, Osp::Ui::KeyCode keyCode);

	void OnScreenOn (void);
	void OnScreenOff (void);

public:
	void OnTimerExpired(Osp::Base::Runtime::Timer& timer);
	bool Draw();

private:
	bool InitEGL();
	bool InitGL();
	void DestroyGL();
	void Update();
	void Cleanup();

	void LoadIdentity(Matrix* result);
	void Frustum(Matrix *result, float left, float right, float bottom, float top, float nearZ, float farZ);
	void Perspective(Matrix* result, float fovy, float aspect, float nearZ, float farZ);
	void Translate(Matrix *result, Osp::Graphics::Opengl::GLfloat tx, Osp::Graphics::Opengl::GLfloat ty, Osp::Graphics::Opengl::GLfloat tz);
	void Rotate(Matrix *result, Osp::Graphics::Opengl::GLfloat angle, Osp::Graphics::Opengl::GLfloat x, Osp::Graphics::Opengl::GLfloat y, Osp::Graphics::Opengl::GLfloat z);
	void Multiply(Matrix *result, Matrix *srcA, Matrix *srcB);

private:
	Osp::Graphics::Opengl::EGLDisplay  __eglDisplay;
	Osp::Graphics::Opengl::EGLSurface  __eglSurface;
	Osp::Graphics::Opengl::EGLConfig   __eglConfig;
	Osp::Graphics::Opengl::EGLContext  __eglContext;
	Osp::Graphics::Opengl::GLuint      __programObject;
	Osp::Graphics::Opengl::GLint       __idxPosition;
	Osp::Graphics::Opengl::GLint       __idxColor;
	Osp::Graphics::Opengl::GLint       __idxMVP;
	Osp::Graphics::Opengl::GLfloat     __angle;
	Osp::Base::Runtime::Timer*         __pTimer;
	Matrix                             __matMVP;	

	Osp::Ui::Controls::Form*           __pForm;
};

#endif // __GLESCUBE_H__

